//
//  ViewController.m
//  棒棒哒
//
//  Created by rimi on 15/7/31.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "ViewController.h"
#import "BBDTabbarViewController.h"
#import "CustomButton.h"
#import "Masonry.h"
#import "Config.h"
#import "VCTableViewCell.h"

static NSString *identifier = @"VCCell";

@interface ViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate, UIActionSheetDelegate, UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UIButton *headButton;
@property (strong, nonatomic) UIImagePickerController *picker;
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *titleSection;

- (void)initializeDataSource;
- (void)initializeUserInterFace;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationController.navigationBarHidden = YES;
    
    [self initializeDataSource];
    [self initializeUserInterFace];
    
    BBDTabbarViewController *tabBarVC = [BBDTabbarViewController shareTabBarController];
    [self addChildViewController:tabBarVC];
    [self.view addSubview:tabBarVC.view];
    
    
}

- (void)initializeDataSource {
    self.titleArray = @[@"个人账户",@"我的收藏",@"设置"];
    self.titleSection = @[@"版本信息",@"关于我们",@"意见反馈"];
}

- (void)initializeUserInterFace {
    // 头像
    _headButton = [CustomButton buttonWithFrame:CGRectMake(20, 64, 60, 60) Title:@"" BackGroudImage:[UIImage imageNamed:@"123.png"] BackGroundColor:[UIColor clearColor] TextColor:nil CornerRadius:30 BorderWidth:1 BorderColor:[UIColor whiteColor]];
    _headButton.layer.masksToBounds = YES;
    [_headButton addTarget:self action:@selector(handleHeadButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_headButton];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 50;
    _tableView.scrollEnabled = NO;
    _tableView.backgroundColor = [UIColor clearColor];
    [_tableView registerNib:[UINib nibWithNibName:@"VCTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@110);
        make.left.mas_equalTo(@20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT * 2 / 3));
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.titleLabel.text = _titleArray[indexPath.row];
    } else {
        cell.titleLabel.text = _titleSection[indexPath.row];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"个人信息";
    } else {
        return @"软件信息";
    }
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

#pragma mark - headButton事件
- (void)handleHeadButtonEvent:(UIButton *)sender {
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    [action showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            [self localPhoto];
            break;
            
        default:
            break;
    }
}

- (void)localPhoto {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    if (image != nil) {
        [_headButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
